package com.example.vaccine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.vaccine.VO.PageVO;
import com.example.vaccine.entity.VaccineFeedback;
import com.example.vaccine.mapper.VaccineFeedbackMapper;
import com.example.vaccine.mapper.VaccinesMapper;
import com.example.vaccine.service.VaccineFeedbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Coisn
 * @since 2025-06-12
 */
@Service
public class VaccineFeedbackServiceImpl extends ServiceImpl<VaccineFeedbackMapper, VaccineFeedback> implements VaccineFeedbackService {

    @Autowired
    private VaccineFeedbackMapper vaccineFeedbackMapper;

    @Override
    public PageVO getFeedbacksByTimeRange(String timeRange, Integer page, Integer size) {
        if (page == null || page < 1) {
            page = 1;
        }
        if (size == null || size < 1) {
            size = 5;
        }

        QueryWrapper<VaccineFeedback> wrapper = new QueryWrapper<>();

        // 构建时间范围条件
        if (timeRange != null && !"all".equalsIgnoreCase(timeRange)) {
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime startTime = null;

            if ("7days".equalsIgnoreCase(timeRange)) {
                startTime = now.minusDays(7);
            } else if ("30days".equalsIgnoreCase(timeRange)) {
                startTime = now.minusDays(30);
            }

            if (startTime != null) {
                wrapper.ge("feedback_time", startTime);
            }
        }

        // 分页查询
        Page<VaccineFeedback> pageInfo = new Page<>(page, size);
        Page<VaccineFeedback> resultPage = vaccineFeedbackMapper.selectPage(pageInfo, wrapper);

        // 包装成 PageVO 返回
        PageVO pageVO = new PageVO();
        pageVO.setData(resultPage.getRecords());
        pageVO.setTotal(resultPage.getTotal());

        return pageVO;
    }

    @Override
    public List<VaccineFeedback> listFeedbacksForExport(String timeRange, String search) {
        QueryWrapper<VaccineFeedback> wrapper = new QueryWrapper<>();

        if (!"all".equalsIgnoreCase(timeRange)) {
            LocalDateTime fromDate = null;
            if ("7days".equalsIgnoreCase(timeRange)) {
                fromDate = LocalDateTime.now().minusDays(7);
            } else if ("30days".equalsIgnoreCase(timeRange)) {
                fromDate = LocalDateTime.now().minusDays(30);
            }
            if (fromDate != null) {
                wrapper.ge("feedback_time", fromDate);
            }
        }

        if (search != null && !search.trim().isEmpty()) {
            wrapper.and(w -> w
                    .like("doctor_id", search.trim())
                    .or()
                    .like("vaccine_numbering", search.trim())
            );
        }

        wrapper.orderByDesc("feedback_time");
        return this.list(wrapper);
    }

    @Override
    public List<VaccineFeedback> listByDoctorId(Integer doctorId) {
        QueryWrapper<VaccineFeedback> wrapper = new QueryWrapper<>();
        wrapper.eq("doctor_id", doctorId);
        return vaccineFeedbackMapper.selectList(wrapper);
    }

    @Override
    public boolean existsByDoctorIdAndVaccineNumbering(Integer doctorId, String vaccineNumbering) {
        QueryWrapper<VaccineFeedback> wrapper = new QueryWrapper<>();
        wrapper.eq("doctor_id", doctorId)
                .eq("vaccine_numbering", vaccineNumbering);
        return vaccineFeedbackMapper.selectCount(wrapper) > 0;
    }

    @Override
    public boolean save(VaccineFeedback feedback) {
        return vaccineFeedbackMapper.insert(feedback) > 0;
    }


}
